package com.lyEducation.controllers.dto.system;

import com.lyEducation.controllers.dto.system.userDetails.AbstractUserDto;
import com.lyEducation.entities.system.SysUserEntity;
import com.lyEducation.util.StringUtils;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

@Setter
@Getter
public class InsertSysUserDto extends AbstractUserDto {
  /** 普通注册 0 邮箱注册 1 手机注册 */
  private Integer insertUserType;

  private String nickName;
  private String identityCard;
  //  private Integer age;
  private boolean sex;
  private Timestamp birthTime;
  private String email;
  private String phone;
  private String mobile;
  private String photo;

  //  @Override
  //  public void setName(String name) {
  //    if (getInsertUserType() == null) {
  //      super.setName(StringUtils.trim(name));
  //      return;
  //    }
  //    if (getInsertUserType() == 0) {
  //      super.setName(StringUtils.trim(getEmail()));
  //    } else if (getInsertUserType() == 1) {
  //      super.setName(StringUtils.trim(getMobile()));
  //    } else {
  //      super.setName(StringUtils.trim(name));
  //    }
  //  }

  public void setNickName(String nickName) {
    this.nickName = StringUtils.trim(nickName);
  }

  public void setIdentityCard(String identityCard) {
    this.identityCard = StringUtils.trim(identityCard);
  }

  public void copyParamToSysUser(SysUserEntity sysUserEntity) {
    sysUserEntity.setUsename(this.getName());
    sysUserEntity.setPassword(new BCryptPasswordEncoder().encode(this.getPassword()));
    sysUserEntity.setName(this.getNickName());
    sysUserEntity.setIdentityCard(this.getIdentityCard());
    sysUserEntity.setSex(this.getSex());
    if (this.getBirthTime() != null) {
      sysUserEntity.setBirthTime(this.getBirthTime());
      sysUserEntity.setAge(getAgeByBirth(this.getBirthTime()));
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(this.getBirthTime());
      sysUserEntity.setBirthday(
          (calendar.get((Calendar.MONTH)) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");
    }
    sysUserEntity.setEmail(this.getEmail());
    sysUserEntity.setPhone(this.getPhone());
    sysUserEntity.setMobile(this.getMobile());
    sysUserEntity.setPhoto(this.getPhoto());
  }

  public boolean getSex() {
    return this.sex;
  }

  public int getAgeByBirth(Date birthday) {
    int age = 0;
    try {
      Calendar now = Calendar.getInstance();

      now.setTime(new Date());

      Calendar birth = Calendar.getInstance();
      birth.setTime(birthday);

      if (birth.after(now)) {
        age = 0;
      } else {
        age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
        if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
          age += 1;
        }
      }
    } catch (Exception e) {
      // 兼容性更强,异常后返回数据
      age = 0;
    }
    return age;
  }
}
